﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using PowerCreator.LMS.Web.Project;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Common.Script;

using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.Config;

public partial class sysadmin_DL_Project_UserLearnSituation : PowerCreator.LMS.Web.Base.WebBasePage
{
    PowerCreator.LMS.ViewQuery.ViewQuery opViewQuery = new PowerCreator.LMS.ViewQuery.ViewQuery();
    PowerCreator.LMS.Web.Course.Resource opResource = new PowerCreator.LMS.Web.Course.Resource();
    PowerCreator.LMS.Web.Channel.Channel opChannel = new PowerCreator.LMS.Web.Channel.Channel();
    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    protected string HtmlLastUploadResource = "";
    protected string HtmlPublicCourse = "";
    protected string HtmlMyProjectList = "";
    ProjectStudent opProjectStudent = new ProjectStudent();
    ProjectUser opProjectUser = new ProjectUser();
    NewProjectPlan opProjectPlan = new NewProjectPlan();
    Project opProject = new Project();
    PowerCreator.LMS.Web.CMS.New opNews = new PowerCreator.LMS.Web.CMS.New();
    PowerCreator.LMS.Web.Download.Download opDownload = new PowerCreator.LMS.Web.Download.Download();
    PowerCreator.LMS.ViewQuery.ViewQuery ov = new PowerCreator.LMS.ViewQuery.ViewQuery();
    PowerCreator.LMS.Web.Course.Resource op = new PowerCreator.LMS.Web.Course.Resource();
    PowerCreator.LMS.Web.Course.Course opCourse = new PowerCreator.LMS.Web.Course.Course();

    VOTOClass voClass = new VOTOClass();
    TOClass objClass = new TOClass();
    TOUserOfClass objClassUser = new TOUserOfClass();

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        if (!IsPostBack)
        {
            int CID = 0;
            if (Request.QueryString["CID"] != null)
            {
                CID = int.Parse(Request.QueryString["CID"].ToString());
                ViewState["CID"] = CID.ToString();  
            }
            BinderMyProjects();
            //BinderRPT();
        }
       
    }
    //得到班级用户
    private DataTable GetClsUser()
    {
        string search = GetSearch(int.Parse(ViewState["CID"].ToString()));
        DataTable dt = opViewQuery.Query(search);
        return dt;
    }

    private string GetSearch(int cid)
    {
        string search = "SELECT TO_UserOfClass.UOC_ID, TO_UserOfClass.UserID,  TO_Learner.UserName, TO_Learner.TeacherId, TO_Learner.Name,  TO_Learner.UserGroup, TO_Learner.UserPIC, TO_Learner.Mobile,  TO_Learner.Email, TO_Learner.ZhiCheng, TO_Learner.ZhiWu,  TO_Learner.Education,TO_UserOfClass.IsCourseFDY FROM TO_Learner ,TO_UserOfClass where TO_Learner.Learner_Id = TO_UserOfClass.UserID  and TO_UserOfClass.ClassID=" + cid;
        if (!string.IsNullOrEmpty(txt_KeyWord.Text))
            search += string.Format(" and ( TO_Learner.Name like '%{0}%' or  TO_Learner.UserName like '%{0}%')", txt_KeyWord.Text.Trim());
        return search;
    }
    private void BinderMyProjects()
    {
        DataTable dtUser = GetClsUser();
        dtUser.Columns.Add("Batch_xueshi");
        dtUser.Columns.Add("bixiukeshi");
        dtUser.Columns.Add("xuanxiukeshi");
        dtUser.Columns.Add("wanchengdu");
        dtUser.Columns.Add("CourseList_Id");
        dtUser.Columns.Add("ClassID");
        for (int k = 0; k < dtUser.Rows.Count; k++)
        {
            VOTOCourseList vocourselist = new VOTOCourseList();
            TOCourseList objcourselist = new TOCourseList();
            PowerCreator.LMS.ViewQuery.ViewQuery obj = new PowerCreator.LMS.ViewQuery.ViewQuery();
            string search = "SELECT TO_Class.Name, TO_Class.Plans_Id, TO_Class.Batch_Id, TO_Class.[Rule], TO_UserOfClass.UserID, TO_UserOfClass.ClassID,TO_Class.ClassCourseListID, TO_CourseList.CourseList_Id, TO_CourseList.CourseListName,TO_Class.ClassLecturer, TO_Class.ClassLeader, TO_Class.ClassAssistant,TO_Batch.Batch_xueshi FROM TO_UserOfClass ,TO_Class,TO_CourseList,TO_Batch Where TO_UserOfClass.ClassID = TO_Class.Class_Id AND TO_Class.ClassCourseListID = TO_CourseList.CourseList_Id and TO_Batch.Batch_Id=TO_CourseList.Batch_Id and TO_CourseList.Used=1 and TO_UserOfClass.UserID=" + dtUser.Rows[k]["UserID"].ToString();
            DataTable dt = new DataTable();
            dt = obj.Query(search);
            dt.Columns.Add("bixiukeshi");
            dt.Columns.Add("xuanxiukeshi");
            dt.Columns.Add("wanchengdu");
           // dt.Columns.Add("UserName");
            string str_searchBX = string.Empty; //查询必修课时
            string str_searchYX = string.Empty;//查询已经学习了的课时
            string str_searchXX = string.Empty;//查询选修课时
            string str_courseid = "0";
            int int_zongkeshi = 0;
            int int_bixiukeshi = 0;
            int int_yixuekeshi = 0;
            int int_xuanxiukeshi = 0;
            DataTable dt_bixiu = new DataTable();
            DataTable dt_xuanxiu = new DataTable();
            
            DataTable dt_yixue = new DataTable();
            float flt_wanchengdu = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                str_searchBX = "select isnull(sum(isnull(CoursePeriod,0)),0)as CoursePeriod from TO_Course where Course_Id in ({0})";
                vocourselist = objcourselist.Load(int.Parse(dt.Rows[i]["CourseList_Id"].ToString()));
                if (!string.IsNullOrEmpty(vocourselist.CourseID))
                {
                    string[] strscourseid = vocourselist.CourseID.Split(',');

                    if (strscourseid.Length > 0)
                    {
                        for (int j = 0; j < strscourseid.Length; j++)
                        {
                            string[] courseid = strscourseid[j].Split('#');
                            str_courseid += "," + courseid[0].ToString();
                        }
                    }
                }
                else
                    str_courseid = "0";
                str_searchBX = string.Format(str_searchBX, str_courseid);
                dt_bixiu = obj.Query(str_searchBX);
                if (dt_bixiu.Rows.Count > 0)
                {
                    int_bixiukeshi = int.Parse(dt_bixiu.Rows[0]["CoursePeriod"].ToString().Trim());
                }
                dt.Rows[i]["bixiukeshi"] = int_bixiukeshi.ToString();

                int_zongkeshi = int.Parse(dt.Rows[i]["Batch_xueshi"].ToString().Trim()) * 3600;
                //str_searchXX = "select isnull(sum(CoursePeriod),0)as CoursePeriod from (select Course_Id,UCUserID,(case when SscrlogTotalTime>=CoursePeriod then CoursePeriod else SscrlogTotalTime end)as CoursePeriod from (select Course_Id,UserCourse.UCUserID,isnull(CoursePeriod,0)*3600 as CoursePeriod,sum(SscrlogTotalTime)as SscrlogTotalTime from TStudentStudyCouReslog left join TO_Course on TO_Course.Course_Id=TStudentStudyCouReslog.FKSscrlogCourseID inner join UserCourse on TO_Course.Course_Id=UserCourse.FKUCCourseID and TStudentStudyCouReslog.FKSscrlogUserID=UserCourse.UCUserID group by Course_Id,UserCourse.UCUserID,isnull(CoursePeriod,0)*3600)as a)as b where UCUserID={0}";
                str_searchXX = "select isnull(sum(CoursePeriod),0)as CoursePeriod from TO_Course where Course_Id  in (select FKUCCourseID from UserCourse where UCUserID={0} and FKUCClassID=" + ViewState["CID"].ToString() + " and UCState=1 )";
                str_searchXX = string.Format(str_searchXX, dtUser.Rows[k]["UserID"]);
                dt_xuanxiu = obj.Query(str_searchXX);
                if (dt_xuanxiu.Rows.Count > 0)
                    int_xuanxiukeshi = int.Parse(dt_xuanxiu.Rows[0]["CoursePeriod"].ToString().Trim());
                else
                    int_xuanxiukeshi = 0;

                dt.Rows[i]["xuanxiukeshi"] = int_xuanxiukeshi.ToString();

                str_searchYX = "select isnull(sum(CoursePeriod),0)as CoursePeriod from (select Course_Id,(case when SscrlogTotalTime>=CoursePeriod then CoursePeriod else SscrlogTotalTime end)as CoursePeriod from (select Course_Id,isnull(CoursePeriod,0)*3600 as CoursePeriod,sum(SscrlogTotalTime)as SscrlogTotalTime from TO_Course,TStudentStudyCouReslog where Course_Id=FKSscrlogCourseID and FKSscrlogUserID={0} and FKSscrlogTrainclaID={1} group by Course_Id,isnull(CoursePeriod,0)*3600 )as a)as b where Course_Id in ({2})";
                str_searchYX = string.Format(str_searchYX, dt.Rows[i]["UserID"].ToString(), dt.Rows[i]["ClassID"].ToString(), str_courseid);
                dt_yixue = obj.Query(str_searchYX);
                if (dt_yixue.Rows.Count > 0)
                    int_yixuekeshi = int.Parse(dt_yixue.Rows[0]["CoursePeriod"].ToString().Trim());

                int_yixuekeshi = int_yixuekeshi + int_xuanxiukeshi;
                if (int_yixuekeshi >= int_zongkeshi)
                    dt.Rows[i]["wanchengdu"] = "100%";
                else
                {
                    if (int_zongkeshi == 0)
                        dt.Rows[i]["wanchengdu"] = "0%";
                    else
                    {

                        flt_wanchengdu = (float)int_yixuekeshi / (float)int_zongkeshi;

                        dt.Rows[i]["wanchengdu"] = flt_wanchengdu.ToString("p");
                    }
                }
                
                //dt.Rows[i]["UserName"] = dtUser.Rows[k]["UserName"].ToString();
                dtUser.Rows[k]["Batch_xueshi"] = dt.Rows[i]["Batch_xueshi"];
                dtUser.Rows[k]["bixiukeshi"] = dt.Rows[i]["bixiukeshi"];
                dtUser.Rows[k]["xuanxiukeshi"] = dt.Rows[i]["xuanxiukeshi"];
                dtUser.Rows[k]["wanchengdu"] = dt.Rows[i]["wanchengdu"];
                dtUser.Rows[k]["CourseList_Id"] = dt.Rows[i]["CourseList_Id"];
                dtUser.Rows[k]["ClassID"] = dt.Rows[i]["ClassID"];
            }
            
        }
        rptNew.DataSource = dtUser;
        rptNew.DataBind();
    }


    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string search = GetSearch(int.Parse(ViewState["CID"].ToString()));
        BinderMyProjects();
    }
    protected void rptNew_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "jsyt")
        {
            Label lbl_Username = (Label)e.Item.FindControl("lbl_Username");
            Label lbl_CourseListID = (Label)e.Item.FindControl("lbl_CourseListID");  //课程表ID
            Label lbl_UserID = (Label)e.Item.FindControl("lbl_UserId");
            string url = "LearnDetailed.aspx?ClassID=" + e.CommandArgument.ToString() + "&CourseListID=" + lbl_CourseListID.Text + "&UserID=" + lbl_UserID.Text + "&UserName=" + lbl_Username.Text;
            PowerCreator.LMS.Common.Script.Script.RedirectURL(url);
        }
    }
    protected void rptNew_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lnbtn_jsyt = (LinkButton)e.Item.FindControl("lnbtn_jsyt");
            Label lbl_CourseListID = (Label)e.Item.FindControl("lbl_CourseListID");

            if (lbl_CourseListID.Text == "0" || lbl_CourseListID.Text=="")
            {
                lnbtn_jsyt.Enabled = false;
            }
        }
    }
}
